package com.qiya.LiaoDa.Fragment;


import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.qiya.LiaoDa.R;
import com.qiya.LiaoDa.kb.DBOperate;
import com.qiya.LiaoDa.kb.MyAdapter;
import com.qiya.LiaoDa.kb.SP;
import com.qiya.LiaoDa.kb.SearchActivity;


import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class ShouYeFragment extends Fragment implements View.OnClickListener {
    private ListView lv_stulist;
    private EditText et_id;
    private EditText et_name;
    private EditText et_sex;
    private EditText et_age;
    private EditText et_search;
    private Button btn_add;
    private Button btn_search;
    private List<SP> list = new ArrayList<SP>();
    DBOperate dbOperate = new DBOperate();

    public ShouYeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shou_ye, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        lv_stulist = (ListView)getActivity(). findViewById(R.id.lv_stulist);
        et_id = (EditText)getActivity().  findViewById(R.id.et_id);
        et_name = (EditText) getActivity(). findViewById(R.id.et_name);
        et_sex = (EditText) getActivity(). findViewById(R.id.et_sex);
        et_age = (EditText) getActivity(). findViewById(R.id.et_age);
        et_search = (EditText) getActivity(). findViewById(R.id.et_search);
        btn_add = (Button) getActivity(). findViewById(R.id.btn_add);
        btn_search = (Button) getActivity(). findViewById(R.id.btn_search);
        btn_add.setOnClickListener(this);
        btn_search.setOnClickListener(this);
        dbOperate.test(getActivity());
        list = dbOperate.queryAll();
        if (!list.isEmpty()) {
            showList(list);
        }
    }

    private void showList(List<SP> stus) {
        /*List<Map<String,Object>> stuMap=new ArrayList<Map<String,Object>>();
        for(student s:stus){
            Map<String,Object> m=new HashMap<String,Object>();
            m.put("id",s.getId());
            m.put("name",s.getName());
            m.put("sex",s.getSex());
            m.put("age",s.getAge());
            stuMap.add(m);
        }
        String[] key={"id","name","sex","age"};
        int[] value={R.id.tv_stuID,R.id.tv_stuName,R.id.tv_stuSex,R.id.tv_stuAge};*/
//        SimpleAdapter simpleAdapter=new SimpleAdapter(this,stuMap,R.layout.stulist,key,value);
//        lv_stulist.setAdapter(simpleAdapter);
        MyAdapter myAdapter = new MyAdapter(stus, getActivity());
        lv_stulist.setAdapter(myAdapter);
    }

    @Override
    public void onClick(View v) {

        if ((! TextUtils.isEmpty(et_id.getText()) )
                && (! TextUtils.isEmpty(et_name.getText()) )&&
                (! TextUtils.isEmpty(et_age.getText()) )&&
                (! TextUtils.isEmpty(et_sex.getText()) )) {
            if (v.getId() == R.id.btn_add) {
                int id = Integer.parseInt(et_id.getText().toString());
                String name = et_name.getText().toString();
                String sex = et_sex.getText().toString();
                int age = Integer.parseInt(et_age.getText().toString());
                dbOperate.insert(new SP(id, name, sex, age));
                list = dbOperate.queryAll();
                if (!list.isEmpty()) {
                    showList(list);
                }
            }
        }else {
            Toast.makeText(getActivity(), "请输入必要的信息", Toast.LENGTH_SHORT).show();
        }

        if ((! TextUtils.isEmpty(et_search.getText()) )) {
            if (v.getId() == R.id.btn_search) {
                String search = et_search.getText().toString();
                if (search.isEmpty()) return;
                Intent intent = new Intent();
                intent.setClass(getActivity(), SearchActivity.class);
                intent.putExtra("searchKey", search);
                startActivity(intent);
            }
        }else {
            Toast.makeText(getActivity(), "添加成功！", Toast.LENGTH_SHORT).show();
        }
    }
}